<script setup lang="ts" name="Config">
import editPage from "./edit.vue";
import detailPage from "./detail.vue";
import { configRefreshCacheApi, delConfigApi, listConfigApi } from "@/api/system/config";

const { proxy } = getCurrentInstance();

const { sys_yes_no } = useDict("sys_yes_no");

// 遮罩层
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryConfigRef = ref(null);

// 表单Id
const id = ref(null);

// 选中数组
const selection = ref<ConfigRecord[]>([]);

// 非单个禁用
const single = ref(true);

// 非多个禁用
const multiple = ref(true);

// 切换搜索条件
const showSearch = ref(false);

// 参数设置表数据
const configList = ref<ConfigRecord[]>([]);

// 弹出层标题
const title = ref("");

// 编辑弹出层
const openEdit = ref(false);

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  configId: { label: "参数主键", visible: true },
  configName: { label: "参数名称", visible: true },
  configKey: { label: "参数键名", visible: true },
  configValue: { label: "参数键值", visible: true },
  configType: { label: "系统内置", visible: true },
  remark: { label: "备注", visible: true },
  createTime: { label: "创建时间", visible: true },
});

// 创建时间选择
const dateRange = ref<[string, string]>(['', '']);

// 查询参数
const queryParams = ref<ConfigRecord>({
  pageNum: 1,
  pageSize: 10,
  configId: null,
  configName: null,
  configKey: null,
  configValue: null,
  configType: null,
  remark: null,
  createTime: null,
});

/** 查询参数设置列表 */
function getList() {
  loading.value = true;
  listConfigApi(addDateRange(queryParams.value, dateRange.value)).then(response => {
    configList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    configList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    });
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openEdit.value = false;
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryConfigRef.value.resetFields();
  dateRange.value = ['', ''];
  handleQuery();
}

/** 切换按钮操作 */
function changSearch() {
  showSearch.value = !showSearch.value;
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: ConfigRecord[]) {
  selection.value = selectionList;
  single.value = selectionList.length !== 1;
  multiple.value = !selectionList.length;
}

/** 新增按钮操作 */
function handleAdd() {
  id.value = null;
  openEdit.value = true;
  title.value = "新增参数设置";
}

/** 修改按钮操作 */
function handleUpdate(row: ConfigRecord) {
  id.value = row.configId || selection.value.map(item => item.configId);
  openEdit.value = true;
  title.value = "修改参数设置";
}

/** 详情按钮操作 */
function handleDetails(row: ConfigRecord) {
  id.value = row.configId;
  openDetail.value = true;
  title.value = "参数设置详情";
}

/** 删除按钮操作 */
function handleDelete(row: ConfigRecord | any) {
  let ids = [];
  let configNames = [];
  if (row.configId) {
    ids = [row.configId];
    configNames = [row.configName];
  } else {
    selection.value.forEach(item => {
      ids.push(item.configId);
      configNames.push(item.configName);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除参数名称为"' + configNames + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delConfigApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出操作按钮 */
function handleExport() {
  download('system/config/export', {
    ...queryParams.value
  }, `参数设置_${new Date().getTime()}.xlsx`);
}

/** 刷新缓存按钮操作 */
function handleRefreshCache() {
  configRefreshCacheApi().then(() => {
    proxy.$modal.msgSuccess("刷新成功");
  });
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryConfigRef" :inline="true" label-width="68px">
        <el-form-item label="参数名称" prop="configName">
          <el-input
              v-model="queryParams.configName"
              placeholder="请输入参数名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="参数键名" prop="configKey">
          <el-input
              v-model="queryParams.configKey"
              placeholder="请输入参数键名"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="系统内置" prop="configType">
          <el-select
              v-model="queryParams.configType"
              placeholder="请选择系统内置"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_yes_no"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="showSearch">
          <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          参数设置列表
        </div>
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-has-permi="['system:config:add']"
        >新增
        </el-button>
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permi="['system:config:remove']"
        >删除
        </el-button>
        <el-button
            type="warning"
            plain
            icon="Download"
            :loading="exportLoading"
            @click="handleExport"
            v-has-permi="['system:config:export']"
        >导出
        </el-button>
        <el-button
            type="success"
            plain
            icon="Refresh"
            @click="handleRefreshCache"
            v-hasPermi="['system:dict:remove']"
        >刷新缓存
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-loading="loading"
          :data="configList"
          stripe
          v-tablesticky
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55" prop="colNo" v-if="columns.colNo.visible" />
        <el-table-column label="参数主键" prop="configId" align="center" v-if="columns.configId.visible" />
        <el-table-column label="参数名称" prop="configName" align="center" min-width="120" show-overflow-tooltip v-if="columns.configName.visible" />
        <el-table-column label="参数键名" prop="configKey" align="center" min-width="140" show-overflow-tooltip v-if="columns.configKey.visible" />
        <el-table-column label="参数键值" prop="configValue" align="center" min-width="120" v-if="columns.configValue.visible" />
        <el-table-column label="系统内置" prop="configType" align="center" v-if="columns.configType.visible">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.configType" />
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" align="center" min-width="160" show-overflow-tooltip v-if="columns.remark.visible" />
        <el-table-column label="创建时间" prop="createTime" align="center" min-width="160" v-if="columns.createTime.visible" />
        <el-table-column label="操作" align="center" fixed="right" min-width="120">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-has-permi="['system:config:edit']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['system:config:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['system:config:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          open-fixed
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin" />
    </div>

    <!-- 添加或修改参数设置对话框 -->
    <edit-page v-if="openEdit" :id="id" :title="title" @closeDialog="closeDialog" />
    <detail-page v-if="openDetail" :id="id" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>